package test.IO;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import test.config.Config;

public class Output {

	public static String inputStreamToString(final InputStream inputStream) throws IOException
	{
		BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
		String line;
		StringBuffer response = new StringBuffer();
		while ((line = rd.readLine()) != null) {
			response.append(line);
			response.append('\n');
		}
		rd.close();
		return response.toString();
	}
	
	public static void inputStreamToSystemOut(final InputStream inputStream) throws IOException {
		System.out.println(inputStreamToString(inputStream));		
	}
	
	public static void toFile(String filename, String input, boolean append) {
		try {
			FileWriter fstream = new FileWriter(filename,append);
	        BufferedWriter out = new BufferedWriter(fstream);
	        out.write(input+"\n");
	        out.flush();
	        out.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void log(String message) {
		if (Config.isLogToFile()) {
			logToFile(message);
		} else {
			logToConsole(message);
		}
	}
	
	
	public static void logToConsole(String message) {
		System.out.println(message);
	}
	
	public static void logToFile(String message) {
		String filename=Config.getTmpFilePath() + "log.txt";
		
		toFile(filename, message, true);
	}
}
